package com.ocom.netty.mj.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.NettyServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.VerifyAccessVo;
import com.ocom.netty.mj.client.SrvpAccessDeviceClient;
import com.ocom.netty.mj.handler.DataAcceptHandler;
import com.ocom.netty.mj.ropo.TcpServiceRopo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
public class DoorController {

    Logger logger= LoggerFactory.getLogger(DoorController.class);


    @Autowired
    private SrvpAccessDeviceClient accessDeviceClient;

    @PostMapping(NettyServiceUrl.URL_LongRange_OpenDoor)
    public Result openDoor(Long companyId, Long pId, String machNo) throws Exception {

        logger.info("companyId:"+companyId);
        logger.info("pId:"+pId);
        logger.info("machNo:"+machNo);
        //开门tcp数据
//        TcpServiceRopo resp=new TcpServiceRopo();
        //验证开门权限
        VerifyAccessVo verifyAccessVo=new VerifyAccessVo();

        verifyAccessVo.setCompanyId(companyId);
        verifyAccessVo.setMachNo(machNo);
        verifyAccessVo.setType(2);
        verifyAccessVo.setIdnum(pId.toString());
//        verifyAccessVo.setIdnum("1");
        verifyAccessVo.setOperatetype(2);
//        verifyAccessVo.setSerialnumber(openNo);
        verifyAccessVo.setSerialnumber("1234");
        verifyAccessVo.setTime(DateUtil.getDateTime());
        System.out.println(verifyAccessVo);

        RespMachMessage respMachMessage1=accessDeviceClient.verifyAccess(verifyAccessVo);//
        logger.info("respMachMessage1"+respMachMessage1.toString());
        if(!respMachMessage1.getCode().equals("0000")){
            logger.info(respMachMessage1.toString());
            return ResultUtil.error(ResultEnum.Err_3007);
        }

        //{code='0000', msg='成功', data={result=1, msg=无开门权限, overopentimes=0, balckFlag=0, overdueFlag=0, type=0, idnum=1, permissionsFlag=1}}

        Map<String,Object> map2= JSON.parseObject(JSON.toJSONString(respMachMessage1.getData()), Map.class);
        //响应结果
        String param= "000000000000000000000000000000";

        StringBuilder stringBuilder=new StringBuilder(param);

        if((Integer)map2.get("result")!=0){
            return ResultUtil.error(ResultEnum.Err_3007);
        }else{
            //开门
            TcpServiceRopo tcpServiceRopo=new TcpServiceRopo();
            tcpServiceRopo.setRespTip("02");
            tcpServiceRopo.setCommandType("0043");
            tcpServiceRopo.setCommandLength("10");
            tcpServiceRopo.setCommandParam(machNo+"01");
            //调用开门
            boolean b=DataAcceptHandler.sendMessage(machNo,tcpServiceRopo);
            if(b){
                //存开门记录
                RespMachMessage respMachMessage=accessDeviceClient.saveAccessRecord(companyId,machNo,pId, DateUtil.getDateTime(),1L);//
//                System.out.println(respMachMessage);
                return ResultUtil.success();
            }else{
                logger.info("机器不在线");
                return ResultUtil.error(ResultEnum.Err_3008);
            }
        }
    }
}
